package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.ProduceEquipmentMapper;
import cn.tedu.erp.project.pojo.dto.ProduceEquipmentAddNewDTO;
import cn.tedu.erp.project.pojo.dto.ProduceEquipmentUpdateDTO;
import cn.tedu.erp.project.pojo.entity.ProduceEquipment;
import cn.tedu.erp.project.pojo.vo.ProduceEquipmentListVO;
import cn.tedu.erp.project.pojo.vo.ProduceEquipmentStandardVO;
import cn.tedu.erp.project.service.IProduceEquipmentService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ProduceEquipmentServiceImpl implements IProduceEquipmentService {
    @Autowired
    private ProduceEquipmentMapper produceEquipmentMapper;

    public ProduceEquipmentServiceImpl() {
        log.debug("创建业务类对象：ProduceEquipmentServiceImpl");
    }

    @Override
    public void addNew(ProduceEquipmentAddNewDTO produceEquipmentAddNewDTO) {
        log.debug("开始处理【添加生产设备】的业务，参数：{}", produceEquipmentAddNewDTO);
        // 创建ProduceEquipment对象
        ProduceEquipment produceEquipment = new ProduceEquipment();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到ProduceEquipment对象中
        BeanUtils.copyProperties(produceEquipmentAddNewDTO, produceEquipment);
        // 调用Mapper对象的insert()执行插入生产设备数据
        log.debug("即将执行插入数据，参数：{}", produceEquipment);
        produceEquipmentMapper.insert(produceEquipment);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除生产设备】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        ProduceEquipmentStandardVO produceEquipment = produceEquipmentMapper.getStandardById(id);
        log.debug("根据ID={}检查生产设备数据是否存在，查询结果：{}", id, produceEquipment);
        // 判断查询结果是否为null
        if (produceEquipment == null) {
            // 是：数据不存在，抛出异常
            String message = "删除生产设备失败，尝试删除的生产设备数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        produceEquipmentMapper.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, ProduceEquipmentUpdateDTO produceEquipmentUpdateDTO) {
        log.debug("开始处理【修改生产设备详情】的业务，ID：{}，新数据：{}", id, produceEquipmentUpdateDTO);
        // 调用Mapper对象的getStandardById()执行查询
        ProduceEquipmentStandardVO queryResult = produceEquipmentMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改生产设备详情失败，尝试修改的生产设备数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 创建ProduceEquipment对象
        ProduceEquipment produceEquipment = new ProduceEquipment();
        // 复制属性，设置ID
        BeanUtils.copyProperties(produceEquipmentUpdateDTO, produceEquipment);
        produceEquipment.setId(id);
        // 调用Mapper对象的update()方法执行修改
        produceEquipmentMapper.update(produceEquipment);
    }

    @Override
    public ProduceEquipmentStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询生产设备详情】的业务，参数：{}", id);
        // ProduceEquipmentStandardVO queryResult = produceEquipmentMapper.getStandardById(id);
        ProduceEquipmentStandardVO queryResult = produceEquipmentMapper.getStandardById(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询生产设备详情失败，生产设备数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<ProduceEquipmentListVO> list() {
        log.debug("开始处理【查询生产设备列表】的业务，无参数");
        // List<ProduceEquipmentListVO> list = produceEquipmentMapper.list();
        List<ProduceEquipmentListVO> list = produceEquipmentMapper.list();
        return list;
    }
}
